package com.intel.factorytest.activity;
import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;
import android.view.View;
import com.intel.factorytest.R;
import java.io.IOException;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.BufferedReader;
import java.io.FileReader;
import android.util.Log;


import android.hardware.Camera;

//import android.hardware.Camera2.CameraManager;
//import android.hardware.Camera2.CameraAccessException;
import android.content.pm.PackageManager;
import android.content.pm.FeatureInfo;
import android.os.Build;

import android.hardware.camera2.CameraManager;
import android.hardware.camera2.CameraAccessException;
import android.content.Context;

public class TorchTestActivity extends BaseActivity {

    private static final String TAG = "TorchTestActivity";
    private static final String ON="1";
    private static final String OFF="0";
    Button openTorch;
    Button closeTorch;
    private static final String TORCH_PATH = "/sys/class/flashlight/torch/enable";

    private Camera mCamera = null;
    
    public void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);

         setContentView(R.layout.torch_test_layout);
         initView();
         Button reTest = (Button)findViewById(R.id.repeat_button);
         reTest.setVisibility(View.GONE);
         mSucBtn.setVisibility(View.INVISIBLE);
         openTorch = (Button)findViewById(R.id.open_torch);
         closeTorch = (Button)findViewById(R.id.close_torch);
       //  closeTorch.setEnabled(false);




         openTorch.setOnClickListener(new View.OnClickListener() {
             @Override
             public void onClick(View v) {
                 //String status = readTorchStatus();
                 //if(ON.equals(status)){
                 //    return;
                 //}
                 //powerOnOffTorch(ON);
                 // lightSwitch(true);
                torchSwitch(true);
                 closeTorch.setEnabled(true);
                 //openTorch.setEnabled(false);

             }
         });


         closeTorch.setOnClickListener(new View.OnClickListener(){
             @Override
             public void onClick(View view){
                 //String status = readTorchStatus();
                 //if(OFF.equals(status)){
                 //    return;
                 //}
                 //powerOnOffTorch(OFF);
                // closeTorch.setEnabled(false);
                // lightSwitch(false);
                torchSwitch(false);
                 openTorch.setEnabled(true);
                 mSucBtn.setVisibility(View.VISIBLE);
             }
          });
          mItemTestFinish = true;//add by wangsm        
        }

    private void powerOnOffTorch(String on){
         try {
             BufferedWriter bufWriter = null;
             bufWriter = new BufferedWriter(new FileWriter(TORCH_PATH));
             bufWriter.write(on);
             bufWriter.close();
         } catch (IOException e) {
             e.printStackTrace();
         }
     }

    private String readTorchStatus(){
        String value = "0";
        try {
            BufferedReader bufReader = null;
            bufReader = new BufferedReader(new FileReader(TORCH_PATH));
            value = bufReader.readLine();
            Log.d(TAG, "readTorchStatus value is " + value);
            bufReader.close();

        } catch (IOException e) {
            e.printStackTrace();
        }
        return value;
    }


    protected void initView() {
        super.initView();
    }

    @Override
    public void onDestroy() {
     super.onDestroy();
     //String status = readTorchStatus();
     //if(ON.equals(status)){
     //    powerOnOffTorch(OFF);
     //}
     // lightSwitch(false);
     torchSwitch(false);
    }

    /**
     *
     * @param lightStatus
     * @return
     */
    private void lightSwitch(final boolean lightStatus) {
        if (!lightStatus) {
            if (mCamera != null) {
                mCamera.stopPreview();
                mCamera.release();
                mCamera = null;
            }
        } else {
            final PackageManager pm = getPackageManager();
            final FeatureInfo[] features = pm.getSystemAvailableFeatures();
            for (final FeatureInfo f : features) {
                if (PackageManager.FEATURE_CAMERA_FLASH.equals(f.name)) {
                    if (null == mCamera) {
                        int num = android.hardware.Camera.getNumberOfCameras();
                        Log.i("hugh-tag", "lightSwitch num=" + num);
                        if (num == 2) {
                            try {
                                mCamera = android.hardware.Camera.open(0);
                            } catch (Exception e) {
                            }
                        }
                    }
                    final Camera.Parameters parameters = mCamera.getParameters();
                    parameters.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
                    mCamera.setParameters(parameters);
                    mCamera.startPreview();
                }
            }
        }
    }    

    private void torchSwitch(boolean torchSwitch) {
        CameraManager manager = (CameraManager) getSystemService(Context.CAMERA_SERVICE);
        try {
            manager.setTorchMode("0", torchSwitch);
        } catch (CameraAccessException e) {
            e.printStackTrace();
        }
    }

}
